import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '@/views/login.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import(/* webpackChunkName: "login" */ '@/views/home.vue'),
    redirect: '/home/welcome',
    children: [
      { path: 'welcome', name: 'welcome', component: () => import(/* webpackChunkName: "login" */ '@/views/welcome.vue') },
      { path: 'users', name: 'users', component: () => import(/* webpackChunkName: "user" */ '@/views/user') },
      { path: 'rights', name: 'rights', component: () => import(/* webpackChunkName: "rights" */ '@/views/power/rights.vue') },
      { path: 'roles', name: 'roles', component: () => import(/* webpackChunkName: "roles" */ '@/views/power/roles.vue') },
      { path: 'categories', name: 'categories', component: () => import(/* webpackChunkName: "goods_cate" */ '@/views/goods/cate.vue') },
      { path: 'params', name: 'params', component: () => import(/* webpackChunkName: "goods_params" */ '@/views/goods/params.vue') },
      { path: 'goods', name: 'goods', component: () => import(/* webpackChunkName: "goods_list" */ '@/views/goods/lists.vue') },
      { path: 'goods/add', name: 'add', component: () => import(/* webpackChunkName: "addGoods" */ '@/views/goods/addGoods.vue') },
      { path: 'orders', name: 'orders', component: () => import(/* webpackChunkName: "order" */ '@/views/order') },
      { path: 'reports', name: 'reports', component: () => import(/* webpackChunkName: "reports" */ '@/views/report') }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // console.log(to)to.matched[0].path === '/home'
  sessionStorage.setItem('active', to.path)
  if (to.matched[0].path === '/home') {
    const token = sessionStorage.getItem('token')
    if (!token) {
      next('/login')
    }
  }
  next()
})

export default router
